"""
根据xls 生成卓岚网关配置脚本
xls 地址：./寄存器、网关json、iotjson标示符使用表.xls
输出：寄存器.json
xls扫描行数范围：3~44
"""

from xlrd import open_workbook

import csv

# 读取到的行数
cell = 48

def addData(r):
    if (sheet.cell(r, 9).value == 'bool'):
        # 4.1.
        zlanboolData[0] = "4." + str(r - 2) + "."
        # m300
        zlanboolData[1] = sheet.cell(r, 2).value
        # 300 地址
        zlanboolData[6] = int(sheet.cell(r, 3).value)
        print(zlanboolData)
        writer.writerow(zlanboolData)
    else:
        # 4.1.
        zlanintData[0] = "4." + str(r - 2) + "."
        # m300
        zlanintData[1] = sheet.cell(r, 2).value
        # 300 地址
        zlanintData[6] = int(sheet.cell(r, 3).value)
        print(zlanintData)
        writer.writerow(zlanintData)


workbook = open_workbook(r'./寄存器、网关json、iotjson标示符使用表.xls')  # 打开xls文件
sheet_name = workbook.sheet_names()  # 打印所有sheet名称，是个列表
sheet = workbook.sheet_by_index(0)  # 根据sheet索引读取sheet中的所有内容

with open('./JSON0091.csv', "a", newline='') as json009Write:
    writer = csv.writer(json009Write, dialect='excel')
    # bool 类型配置
    zlanboolData = ['4.1.', 'm300', 'Modbus RTU', 'thing.service.property.set', 2, 1, 300, 1, 9410, 2, 0, '布尔型', 0, 0,
                    '',
                    0, 100, 97, 0, 0, 0, 1, 0, 255]
    # int 类型配置
    zlanintData = ['4.1.', 'm300', 'Modbus RTU', 'thing.service.property.set', 2, 3, 300, 1, 9410, 2, 0, '无符号整型', 0, 0,
                   '',
                   0, 100, 97, 0, 0, 0, 1, 0, 255]

    # 扫描范围：3~44行
    for i in range(0, int((cell - 3) / 2)):
        # 偶寄添加顺序
        print(2*i + 3)
        print(2*i + 4)
        r = 2*i + 3
        r1 = 2 * i + 4
        addData(r1)
        addData(r)
    if(((cell - 3) % 2) != 0):
        addData(cell - 1)
